import type { UploadFileInfo } from 'naive-ui';
import i18n from '@/i18n';

const { t } = i18n.global;
/** 防抖
 * @param func  寻妖防抖的函数
 * @param delay 延迟
 * @returns 防抖处理后的函数
 */
export function debounce(func: Function, delay: number) {
	let timer: any = null;
	return function (params?) {
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(() => {
			func(params);
		}, delay);
	};
}

/** 图片上传前检测 */
export function imgBeforeUpload(data: { file: UploadFileInfo; fileList: UploadFileInfo[] }) {
	const message = window['$message'];
	const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
	if (!arr.includes(data.file.file!.type)) {
		message.error(t('msg.uploadImgType'));
		return false;
	}
	if (data.file.file!.size > 1024 * 1024 * 10) {
		message.error(t('msg.uploadSize'));
		return false;
	}
	return true;
}
